<script type="text/javascript" src="lib.js"></script>
<script type="text/javascript" src="extract-content.js"></script>
<script type="text/javascript">
function getPreview(callback, url) {
   var xhr = new XMLHttpRequest();
   xhr.onreadystatechange = function(){
      if(xhr.readyState == 4){
         var ex = new ExtractContentJS.LayeredExtractor();
         ex.addHandler( ex.factory.getHandler('Heuristics') );
	 // var doc = document.createElement('html');
         // doc.innerHTML = xhr.responseText;
         // var doc = (new DOMParser).parseFromString(xhr.responseText, "text/xml");
         var doc = document.implementation.createHTMLDocument(xhr.responseText);
         // alert(doc);
         var res = ex.extract(doc);
         // alert(res);
         alert(res.isSuccess);
         
         if (res.isSuccess) {
            alert(res.content.asTextFragment);
            callback(res.content.asTextFragment);
         }else{
            callback("取得・解析に失敗しました。");
         }
      }
   }
   var getStr = url;
   xhr.open("GET", getStr, false);
   xhr.send();
};

function onRequest(request, sender, callback) {
   // alert(request);
   if (request.action == 'getPreview') {
          // alert(request.url);
          getPreview(callback, request.url);
   }
};
chrome.extension.onRequest.addListener(onRequest);
</script>
